package com.saicmotor.springboot_demo.controller;

import com.saicmotor.springboot_demo.model.User;
import com.saicmotor.springboot_demo.model.json_pojo.JsonResult;
import com.saicmotor.springboot_demo.service.CommonService;
import com.saicmotor.springboot_demo.service.UserRoleService;
import com.saicmotor.springboot_demo.service.UserService;
import com.saicmotor.springboot_demo.config.ValidateGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;

@Validated
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    CommonService commonService;
    @Autowired
    UserRoleService userRoleService;

    @Autowired
    JsonResult jsonResult;

    @GetMapping("/age/{name}")
    public JsonResult getAgeOfUser(@PathVariable(value = "name") String name) {   //http://localhost:8080/user/age?name=setName
        try {
            return jsonResult.data(String.valueOf(userService.getAgeByName(name)));
        } catch (Exception e) {
            log.error("异常地点：springboot_demo.service.userService.getAgeByName(name)", e);
            return jsonResult.msg("DATA DO NOT EXIST");
        }
    }

    @GetMapping("/name/{age}")
    public JsonResult getNameByAge(@PathVariable(value = "age")
                                         @Min(value = 1, message = "age must be bigger than zero") int age) {
        return jsonResult.data(userService.getNameByAge(age));
}

    @GetMapping("/all")
    public JsonResult getAll() {
        return jsonResult.data(userService.getAll());
    }

    @PostMapping("/add")
    public JsonResult insertInfoOfUser(@RequestBody
                                       @Valid
                                       @Validated(ValidateGroup.UserAdd.class) User user) {
        if (commonService.checkName("user", user.getName())) {
            return jsonResult.msg(user.getName() + " is EXISTS");
        } else {
            userService.insertUserInfo(user.getName(), user.getAge());
            return jsonResult.msg("Insert [" + user.getName() + "] Complete");
        }
    }

    @DeleteMapping("/delete/name")
    public JsonResult deleteUser(@RequestBody
                                 @Validated(ValidateGroup.UserNameDelete.class) User user) {
        if(!commonService.checkName("user",user.getName())){
            return jsonResult.msg("[" + user.getName() + "] Is Not Exist");
        }else {
            userService.deleteByName(user.getName());
            userRoleService.deleteByUserName(user.getName());
            return jsonResult.msg("Delete [" + user.getName() + "] Complete");
        }
    }

    @DeleteMapping("/delete/age")
    public JsonResult deleteAge(@RequestBody
                                @Valid@Validated(ValidateGroup.UserAgeDelete.class) User user) {
        userService.deleteByAge(user.getAge());
        userService.getNameByAge(user.getAge()).forEach(name -> {
            userRoleService.deleteByUserName(name);
        });
        return jsonResult.msg("Delete Age Of [" + user.getAge() + "] Complete");
    }

    @PutMapping("/update")
    public JsonResult updateAgeOfUser(@RequestBody
                                      @Valid
                                      @Validated(ValidateGroup.UserAdd.class) User user) {
        if (!commonService.checkName("user", user.getName())) {
            return jsonResult.msg(user.getName() + " Is Not Exist");
        } else {
            userService.updateAgeByName(user.getName(), user.getAge());
            return jsonResult.msg("Update [" + user.getName() + "] Complete");
        }
    }

    @PostMapping("/create")
    public JsonResult userTableCreation() {
        if (commonService.checkTable("user")) {
            return jsonResult.msg("[user] Table Exists");
        } else {
            userService.createUserTable("user");
            return jsonResult.msg("Create [user] Table Complete");
        }
    }

    @PostMapping("/create/init")
    public JsonResult initUserTable() {
        commonService.cleanTable("user");
        userService.initUserTable();

        return jsonResult.msg("Init [user] Table Complete");
    }
}
